package com.yanqu.road.server.cmd.activity.stronghold.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eChatContentType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.pb.WorldMessagePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_CROSS_STRONGHOLD_MAMMON_HELP_NOTIFY, desc = "")
public class CrossStrongholdMammonHelpNotifyCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossStrongholdMammonHelpRespMsg msg = StrongholdWarProto.CrossStrongholdMammonHelpRespMsg.parseFrom(packet.getBytes());
        int goodsId = msg.getGoodsId();
        ret = msg.getRet();
        int mammonType = msg.getMammonType();
        StrongholdWarProto.MammonHelpRespMsg.Builder resp = StrongholdWarProto.MammonHelpRespMsg.newBuilder();
        resp.setRet(ret);
        if (ret == 0) {
            player.getModule(CurrencyModule.class).removeCurrency(new Property(goodsId, BigInteger.ONE), eLogMoneyType.Stronghold, eLogMoneyType.StrongholdMammonHelp);
            StrongholdWarProto.CrossStrongholdMammonHelpDataMsg result = msg.getResult();
            Property killReward = new Property();
            for (int i = 0; i < result.getKillNum(); i++) {
                killReward.addProperty(StrongholdActivityMgr.getEliminateKillReward());
            }
            player.getModule(CurrencyModule.class).addCurrency(killReward, eLogMoneyType.Stronghold, eLogMoneyType.StrongholdMammonHelp);

            StrongholdWarProto.MammonHelpDataMsg.Builder dataMsg = StrongholdWarProto.MammonHelpDataMsg.newBuilder();
            dataMsg.setKillReward(PropertyHelper.parsePropertyToString(killReward));
            dataMsg.setAttackUnionNum(result.getAttackUnionNum());
            dataMsg.setKillNum(result.getKillNum());
            dataMsg.setTotalDamage(result.getTotalDamage());
            dataMsg.addAllDamageDetail(result.getDamageDetailList());
            Property useReward = StrongholdActivityMgr.getMammonUseReward(goodsId);
            player.getModule(CurrencyModule.class).addCurrency(useReward, eLogMoneyType.Stronghold, eLogMoneyType.StrongholdMammonHelp);
            dataMsg.setUseReward(PropertyHelper.parsePropertyToString(useReward));
            // 发聊天公告
            resp.setData(dataMsg);
            player.getModule(StrongholdModule.class).addDamage(new BigInteger(result.getTotalDamage()));
            noticeAllServer(player, mammonType, result);
        }
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_ELIMINATION_MAMMON_HELP, resp);
    }

    private void noticeAllServer(GamePlayer player, int mammonType, StrongholdWarProto.CrossStrongholdMammonHelpDataMsg result) {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo != null) {
            String content = WorldMessageMgr.encodeContent(mammonType, result.getAttackUnionNum(), result.getKillNum(), result.getTotalDamage(),result.getAttackUnionName());
            WorldMessageProto.WorldMessageTempMsg.Builder talkMsg = WorldMessagePb.parseWorldMessageTempMsg(player.getUserInfo(), eMessageType.StrongholdMammonHelp.getValue(), eChatContentType.Common.getContentType(), content, System.currentTimeMillis(), activityInfo.getActivityId(), null);
            WorldMessageMgr.sendCrossActivityChat(talkMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
